package modeleditor.handler;

import modeleditor.View;

import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.core.commands.IHandler;
import org.eclipse.core.commands.IHandlerListener;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.handlers.HandlerUtil;

public class CallEditor implements IHandler {

	@Override
	public void addHandlerListener(IHandlerListener handlerListener) {
		// TODO Auto-generated method stub

	}

	@Override
	public void dispose() {
		// TODO Auto-generated method stub

	}

	@Override
	public Object execute(ExecutionEvent event) throws ExecutionException {
		 System.out.println("called");
		    // Get the view
		    IWorkbenchWindow window = HandlerUtil.getActiveWorkbenchWindow(event);
		    IWorkbenchPage page = window.getActivePage();
		    View view = (View) page.findView(View.ID);
		    // Get the selection
		    ISelection selection = view.getSite().getSelectionProvider()
		        .getSelection();
		    if (selection != null && selection instanceof IStructuredSelection) {
		      Object obj = ((IStructuredSelection) selection).getFirstElement();
		      // If we had a selection lets open the editor
		      if (obj != null) {
		    	  /*
		        Person person = (Person) obj;
		        MyPersonEditorInput input = new MyPersonEditorInput(person.getId());
		        try {
		          page.openEditor(input, MyPersonEditor.ID);

		        } catch (PartInitException e) {
		          throw new RuntimeException(e);
		        }
		        */
		      }
		    }
		return null;
	}

	@Override
	public boolean isEnabled() {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean isHandled() {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public void removeHandlerListener(IHandlerListener handlerListener) {
		// TODO Auto-generated method stub

	}

}
